/**
 * 作者: sessionliang
 * 时间: 2017-08-20
 * 描述: 下拉刷新 通用父页面 
 * 用户信息父页面
 */
define(function(require, exports, module) {
	"use strict";
	//每一个页面都要引入的工具类
	var CommonTools = require('CommonTools_Core');
	var WindowTools = require('WindowTools_Core');
	//轮播图工具类
	var GallerySliderTools = require('GallerySliderTools_Core');

	//base service
	var baseService = require('Base_Service');
	var registerService = require('Register_Service');
	var orderService = require('Order_Service');
	var enumHelper = require('Common_Enum');

	var model = null;
	var payType = 'ali';
	var wxChannel = null; // 微信支付  
	var aliChannel = null; // 支付宝支付  

	//每一个页面都要引入的工具类
	// initready 要在所有变量初始化做完毕后
	CommonTools.initReady(initData);
	/**
	 * @description 初始化数据,结合initReady使用
	 * plus情况为plusready
	 * 其它情况为直接初始化
	 */
	function initData() {
		//引入必备文件,下拉刷新依赖于mui与mustache
		CommonTools.importFile([
			'js/libs/mui.min.js'
		], function() {
			CommonTools.plusReady(function() {
				plus.payment.getChannels(function(channels) {
					for(var i in channels) {
						if(channels[i].id == "wxpay") {
							wxChannel = channels[i];
						} else {
							aliChannel = channels[i];
						}
					}
				}, function(e) {
					alert("获取支付通道失败：" + e.message);
				});
			});
			initHomePage();
			initListeners();
		});
	}

	/**
	 * @description 监听
	 */
	function initListeners() {
		mui(".paytype").on("tap", ".mui-table-view-cell", function() {
			payType = this.getAttribute('data');
		});
		mui(".mui-content").on("tap", "#btnPay", function() {
			if(model != null) {
				if(payType == 'ali') {
					orderService.alipay(model.Code, function(retval) {
						if(plus) {
							plus.payment.request(aliChannel, retval, function(result) {
								plus.nativeUI.alert("支付成功！", function() {
									WindowTools.createWin(null, "../index.html",null, {});
								});
							}, function(error) {
								plus.nativeUI.alert("支付失败：" + error.code);
							});
						}
					});
				} else if(payType = 'wx') {
					orderService.wxpay(model.Code, function(retval) {
						if(plus) {
							plus.payment.request(wxChannel, retval, function(result) {
								plus.nativeUI.alert("支付成功！", function() {
									WindowTools.createWin(null, "../index.html", null, {});
								});
							}, function(error) {
								plus.nativeUI.alert("支付失败：" + error.code);
							});
						}

					});
				} else {

				}
			}
		});
	}

	/**
	 * @description 渲染頁面
	 */
	function initHomePage() {
		var id = WindowTools.getExtraDataByKey("mid");
		registerService.getApply(id, function(retval) {
			model = retval;
			mui("#Code")[0].innerHTML = retval.Code;
			mui("#DrivingTitle")[0].innerHTML = retval.DrivingTitle;
			mui("#DrivingLicenseType")[0].innerHTML = enumHelper.drivingLicenseTypeFormat(retval.DrivingLicenseType);
			mui("#ClassType")[0].innerHTML = enumHelper.classTypeFormat(retval.ClassType);
			mui("#PayFee")[0].innerHTML = "￥" + retval.PayFee;
		});
	}
});